package com.zysc.processflow.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zysc.processflow.common.base.BaseController;
import com.zysc.processflow.common.base.PageResult;
import com.zysc.processflow.entity.Brand;
import com.zysc.processflow.entity.Category;
import com.zysc.processflow.service.IAttachmentService;
import com.zysc.processflow.service.impl.BrandService;
import com.zysc.processflow.service.impl.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: Mr.Liuyijiang
 * @Description:
 * @Date Created in 2022-06-04 20:23
 * @Modified By:
 */
@Controller
@RequestMapping("/item/brand")
public class BrandController extends BaseController<Brand> {
    @Autowired
    private BrandService brandService;
    @Autowired
    private IAttachmentService attachmentService;

    public BrandController(BrandService service) {
        super(service);
    }



    // http://api.leyou.com/api/item/brand/page?key=&page=1&rows=5&sortBy=id&desc=false

    /**
     * 根据查询条件，分页并排序查询品牌信息
     * @param key
     * @param page
     * @param rows
     * @param sortBy
     * @param desc
     * @return
     */
    @GetMapping("page")
    public ResponseEntity<PageResult<Brand>> queryBrandsByPage(
            @RequestParam(value = "key",required = false) String key,
            @RequestParam(value = "page",defaultValue = "1") Integer page,
            @RequestParam(value = "rows",defaultValue = "5") Integer rows,
            @RequestParam(value = "sortBy",required = false) String sortBy,
            @RequestParam(value = "desc",required = false) Boolean desc
            ){
        List <Brand> list = new ArrayList<>();
        if(page!=null && rows!=null){
            Page<Brand> brandPage = new Page<Brand>(page,rows);
            list = this.brandService.queryBrandsByPage(brandPage,key, sortBy,desc);
            return ResponseEntity.ok(new PageResult(brandPage.getTotal(),list));
        }else{
            list = this.brandService.queryBrandsByPage(null,key, sortBy,desc);
            return ResponseEntity.ok(new PageResult((long)list.size(),list));
        }

    }


    //http://api.leyou.com/api/item/brand
    // {name: "黑马", image: "", cids: "121,305", letter: "H"} 参数是一个json，那么只能使用一个参数去接收
    //解决办法：前端不能传json过来，传普通对象过来，name=黑马&image=""&cids=121,305&letter=H
    //使用this.$qs.stringfy(params) 改成了和get请求差不多的样子,而非params：params,这是json必须加@requestBody
    @PostMapping("saveBrand")
    public ResponseEntity<Void> saveBrand(@RequestBody Brand brand){

        List<Long> cids = new ArrayList<>();
        if(brand.getCids()!=null){
            String[] cidsStr = brand.getCids().split(",");
            for (String cidStr:cidsStr){
                cids.add(Long.parseLong(cidStr));
            }
        }
        this.brandService.saveBrand(brand,cids);
        if(brand.getAttatchments()!=null && brand.getAttatchments().size()>0){
            attachmentService.UpdateLinkAttachment(brand.getId(),"brand",brand.getAttatchments());
        }


        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    /**
     * 根据查询条件，分页并排序查询品牌信息
     * @return
     */
    @GetMapping("cid/{cid}")
    public ResponseEntity<List<Brand>> queryBrandsByCid(@PathVariable("cid") Long cid){
        List<Brand> brands  = this.brandService.queryBrandsByCid(cid);
        if(CollectionUtils.isEmpty(brands)){
            return ResponseEntity.notFound().build();
        }
        return ResponseEntity.ok(brands);
    }

    @GetMapping("bid/{bid}")
    public ResponseEntity<Brand> queryByBid(@PathVariable("bid") Long bid){
        Brand categoryServiceById = brandService.getById(bid);
        if (categoryServiceById == null){
            return ResponseEntity.notFound().build();
        }else {
            return ResponseEntity.ok(categoryServiceById);
        }
    }
}
